// pages/Orders/OrderDetail/OrderDetail.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryGoodsRiderInfo , getGoodsOrderExpressInfo } from '../../../api/order';
var QQMapWX = require('../../../common/qqmap-wx-jssdk.min.js');
var qqmapsdk;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    MapContext: {}, // 地图实例
    local: {}, //经纬度
    order: {},
    markers: [],
    points: [],
    isShowAllExpressBtn: true,
    express: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    if(options.item){
      let item = JSON.parse(options.item);
      console.log(item,'item')
      this.setData({
        order: item,
      })
      _this.MapContext = wx.createMapContext("maps", _this);
      if(item.orderStatus == '2' && item.deliveryType == '0'){
        _this._queryGoodsRiderInfo();
      }else{
        var markers = [];
        markers.push({
          id: 0,
          latitude: _this.data.order.receivingAddressInfo.latitude,
          longitude :_this.data.order.receivingAddressInfo.longitude,
          iconPath: `${assetsAddress}/riders/order/warehouseIcon.png`,
          width: 28,
          height: 34,
        })
        _this.setData({
          markers: markers,
        })
      }
      if(item.deliveryType == 1 && item.orderStatus == '2'){
        _this._getGoodsOrderExpressInfo();
      }
      console.log(item.receivingAddressInfo,'receivingAddressInfo')
      _this.MapContext.moveToLocation( { longitude :parseFloat(item.receivingAddressInfo.longitude) , latitude : parseFloat(item.receivingAddressInfo.latitude) } )
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if(this.data.timeIntervalSeveral){
      clearTimeout(this.data.timeIntervalSeveral);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 获取骑手信息
  _queryGoodsRiderInfo(){
    let _this = this;
    queryGoodsRiderInfo({orderCode: _this.data.order.orderCode})
    .then(res => {
      if(res.success){
        var markers = [];
        markers.push({
          id: 0,
          latitude: _this.data.order.receivingAddressInfo.latitude,
          longitude :_this.data.order.receivingAddressInfo.longitude,
          iconPath: `${assetsAddress}/riders/order/warehouseIcon.png`,
          width: 28,
          height: 34,
        })
        markers.push({
          id: 1,
          latitude: res.data.latitude,
          longitude : res.data.longitude,
          iconPath: `${assetsAddress}/riders/order/riderIcon.png`,
          width: 28,
          height: 34,
        })
        var points = [{latitude: _this.data.order.receivingAddressInfo.latitude,
          longitude :_this.data.order.receivingAddressInfo.longitude,},{latitude: res.data.latitude,
            longitude : res.data.longitude,}];
            console.log(points,'points')
        _this.setData({
          markers: markers,
          // points: points,
        })
        // 定时器
        if(_this.data.takeTotal > 0){
          var timeIntervalSeveral = setTimeout(_this._getLocation, 180000);
          _this.setData({
            timeIntervalSeveral,
            timeList,
          })
        }else{
          if(_this.data.timeIntervalSeveral){
            clearTimeout(_this.data.timeIntervalSeveral); //清除定时器
          }
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  _getGoodsOrderExpressInfo(){
    let _this = this;
    getGoodsOrderExpressInfo({orderCode: _this.data.order.orderCode})
    .then(res => {
      if(res.success){
        if(res.data.list.length > 0){
          var stepsWrap = [
            {
              text: res.data.list[0].status,
              desc: res.data.list[0].time,
            },
            {
              text: `送至${_this.data.order.receivingAddressInfo.province}${_this.data.order.receivingAddressInfo.city}${_this.data.order.receivingAddressInfo.area}${_this.data.order.receivingAddressInfo.detailedAddress}`,
              desc: `${_this.data.order.receivingAddressInfo.receiver} ${_this.data.order.receivingAddressInfo.receiverPhone}`,
            }
          ]
          var arr = [];
          for (let index = 0; index < res.data.list.length; index++) {
            const item = res.data.list[index];
            var obj = {
              text: item.status,
              desc: item.time,
            }
            arr.push(obj);
          }
          _this.setData({
            stepsWrap: stepsWrap,
            step: arr,
          })
        }
        _this.setData({
          express: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  showAllExpress(){
    let _this = this;
    _this.setData({
      stepsWrap: _this.data.step,
      isShowAllExpressBtn: false,
    })
  },
})